package com.emep.changzhi.analyse.service.information;

import com.emep.changzhi.analyse.entity.information.SysPromptConfig;
import com.emep.changzhi.analyse.repository.information.SysPromptConfigRepository;
import com.emep.changzhi.analyse.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author yangj
 * @version V1.0
 * @Description: 报警配置表
 * @date 2018/2/28  15:27
 */
@Slf4j
@Service
public class SysPromptConfigService {

    @Resource
    private SysPromptConfigRepository sysPromptConfigRepository;

    /**
     *
     * 不含查询条件的分页获取数据
     *
     * @param pageNo
     * @param pageSize
     * @return
     * @throws Exception
     */
    public Page<SysPromptConfig> findAll(Integer pageNo, Integer pageSize){
        try {
            Pageable pageable = new PageRequest(pageNo-1, pageSize);
            return  sysPromptConfigRepository.findAll(pageable);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
            return null;
        }
    }

    /**
     *
     * 新增 报警配置
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public SysPromptConfig save(Map<String, Object> payload) {
        SysPromptConfig sysPromptConfig = null;
        try {
            SysPromptConfig sysPromptConfig1 = (SysPromptConfig) MapUtils.convertMap(SysPromptConfig.class,payload);
            sysPromptConfig = sysPromptConfigRepository.save(sysPromptConfig1);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return sysPromptConfig;
    }


    /**
     *
     * 根据id查询 报警配置
     *
     * @param id
     * @return
     */
    public SysPromptConfig findId(String id){
        SysPromptConfig sysPromptConfig = null;
        try {
            sysPromptConfig = sysPromptConfigRepository.findSysPromptConfigById(id);
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return sysPromptConfig;
    }

    /**
     *
     * 根据id 删除 报警配置
     *
     * @param id
     */
    public Boolean delete(String id) {
        Boolean result = false;
        try {
            Integer result1 = sysPromptConfigRepository.deleteById(id);
            if(result1 == 1){
                result = true;
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return result;
    }

    /**
     *查询所有 报警配置
     *
     * @return List<SysPromptConfig>
     */
    public List<SysPromptConfig> findAll() {
        List<SysPromptConfig> sysPromptConfig = null;
        try {
            sysPromptConfig =  sysPromptConfigRepository.findAll();
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return sysPromptConfig;
    }
}
